package com.econ.powercloud.ems.config;

import com.econ.powercloud.common.base.facade.enums.StatusType;
import com.econ.powercloud.common.base.facade.exception.CommonRuntimeException;
import com.econ.powercloud.common.base.facade.exception.DuplicateUniqueException;
import com.econ.powercloud.common.base.facade.pojo.MsgResult;
import com.mysql.cj.exceptions.ConnectionIsClosedException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.util.stream.Collectors;

/**
 * @Author : zjf
 * @Description :
 * @Date : 2021/12/2 9:50
 * @Modified by : zjf
 */
@RestControllerAdvice
@Slf4j
public class ExceptionEndpointAdvice {

    @ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
    public MsgResult<Object> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
        log.warn("输入参数异常", e);
        return MsgResult.builder().status(StatusType.ILLEGAL_ARGUMENT.getCode()).statusText(e.getMessage()).build();
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public MsgResult<Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.warn("MethodArgumentNotValidException异常", e);
        String errorMsg = e.getBindingResult().getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(","));
        return MsgResult.builder().status(StatusType.ILLEGAL_ARGUMENT.getCode()).statusText(errorMsg).build();
    }

    @ExceptionHandler(value = BindException.class)
    @ResponseBody
    public MsgResult<Object> handleBindException(BindException e) {
        log.warn("BindException异常", e);
        String errorMsg = e.getBindingResult().getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(","));
        return MsgResult.builder().status(StatusType.ILLEGAL_ARGUMENT.getCode()).statusText(errorMsg).build();
    }

    @ExceptionHandler(value = IllegalArgumentException.class)
    public MsgResult<Object> illegalArgumentHandler(IllegalArgumentException ex) {
        log.warn("IllegalArgumentException异常：{}", ex.getMessage());
        return MsgResult.builder().status(StatusType.ILLEGAL_ARGUMENT.getCode()).statusText(ex.getMessage()).build();
    }

    @ExceptionHandler(value = DuplicateUniqueException.class)
    public MsgResult<Object> duplicateUniqueHandler(DuplicateUniqueException ex) {
        return MsgResult.builder().status(StatusType.DUPLICATE_COLUMN.getCode()).statusText(ex.getMessage()).build();
    }

    @ExceptionHandler(value = CommonRuntimeException.class)
    public MsgResult<Object> commonRuntimeHandler(CommonRuntimeException ex) {
        return MsgResult.builder().status(ex.getStatusType().getCode()).statusText(ex.getMessage()).build();
    }

    @ExceptionHandler(value = ConnectionIsClosedException.class)
    public MsgResult<Object> connectionIsClosedException(ConnectionIsClosedException ex){
        return MsgResult.builder().status(StatusType.FAIL.getCode()).statusText(ex.getMessage()).build();
    }

    @ExceptionHandler(value = RuntimeException.class)
    public MsgResult<Object> handleUnknownException(RuntimeException e) {
        log.error("未知异常", e);
        return MsgResult.builder().status(StatusType.FAIL.getCode()).statusText(e.getMessage()).build();
    }
}
